package com.common;

import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;
@Deprecated
/**
 * Created by sola on 2018/1/28.
 */
public class MySort implements Iterable<MySort.Order>, Serializable {

    public static final MySort.Direction DEFAULT_DIRECTION;
    private List<MySort.Order> orders;

    public MySort() {
    }

    public MySort(MySort.Order... orders) {
        this(Arrays.asList(orders));
    }

    public MySort(List<MySort.Order> orders) {
        if (null != orders && !orders.isEmpty()) {
            this.orders = orders;
        } else {
            throw new IllegalArgumentException("You have to provide at least one sort property to sort by!");
        }
    }

    public MySort(String... properties) {
        this(DEFAULT_DIRECTION, properties);
    }

    public MySort(MySort.Direction direction, String... properties) {
        this(direction, (List) (properties == null ? new ArrayList() : Arrays.asList(properties)));
    }

    public MySort(MySort.Direction direction, List<String> properties) {
        if (properties != null && !properties.isEmpty()) {
            this.orders = new ArrayList(properties.size());
            Iterator var3 = properties.iterator();

            while (var3.hasNext()) {
                String property = (String) var3.next();
                this.orders.add(new MySort.Order(direction, property));
            }

        } else {
            throw new IllegalArgumentException("You have to provide at least one property to sort by!");
        }
    }

    public MySort and(MySort sort) {
        if (sort == null) {
            return this;
        } else {
            ArrayList<MySort.Order> these = new ArrayList(this.orders);
            Iterator var3 = sort.iterator();

            while (var3.hasNext()) {
                MySort.Order order = (MySort.Order) var3.next();
                these.add(order);
            }

            return new MySort(these);
        }
    }

    public MySort.Order getOrderFor(String property) {
        Iterator var2 = this.iterator();

        MySort.Order order;
        do {
            if (!var2.hasNext()) {
                return null;
            }

            order = (MySort.Order) var2.next();
        } while (!order.getProperty().equals(property));

        return order;
    }

    @Override
    public Iterator<MySort.Order> iterator() {
        return this.orders.iterator();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (!(obj instanceof MySort)) {
            return false;
        } else {
            MySort that = (MySort) obj;
            return this.orders.equals(that.orders);
        }
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + this.orders.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return StringUtils.collectionToCommaDelimitedString(this.orders);
    }

    static {
        DEFAULT_DIRECTION = MySort.Direction.ASC;
    }

    public static class Order implements Serializable {
        private static final long serialVersionUID = 1522511010900108987L;
        private static final boolean DEFAULT_IGNORE_CASE = false;
        private final MySort.Direction direction;
        private final String property;
        private final boolean ignoreCase;
        private final MySort.NullHandling nullHandling;

        public Order(MySort.Direction direction, String property) {
            this(direction, property, false, (MySort.NullHandling) null);
        }

        public Order(MySort.Direction direction, String property, MySort.NullHandling nullHandlingHint) {
            this(direction, property, false, nullHandlingHint);
        }

        public Order(String property) {
            this(MySort.DEFAULT_DIRECTION, property);
        }

        private Order(MySort.Direction direction, String property, boolean ignoreCase, MySort.NullHandling nullHandling) {
            if (!StringUtils.hasText(property)) {
                throw new IllegalArgumentException("Property must not null or empty!");
            } else {
                this.direction = direction == null ? MySort.DEFAULT_DIRECTION : direction;
                this.property = property;
                this.ignoreCase = ignoreCase;
                this.nullHandling = nullHandling == null ? MySort.NullHandling.NATIVE : nullHandling;
            }
        }

        public MySort.Direction getDirection() {
            return this.direction;
        }

        public String getProperty() {
            return this.property;
        }

        public boolean isAscending() {
            return this.direction.isAscending();
        }

        public boolean isDescending() {
            return this.direction.isDescending();
        }

        public boolean isIgnoreCase() {
            return this.ignoreCase;
        }

        public MySort.Order with(MySort.Direction direction) {
            return new MySort.Order(direction, this.property, this.ignoreCase, this.nullHandling);
        }

        public MySort.Order withProperty(String property) {
            return new MySort.Order(this.direction, property, this.ignoreCase, this.nullHandling);
        }

        public MySort withProperties(String... properties) {
            return new MySort(this.direction, properties);
        }

        public MySort.Order ignoreCase() {
            return new MySort.Order(this.direction, this.property, true, this.nullHandling);
        }

        public MySort.Order with(MySort.NullHandling nullHandling) {
            return new MySort.Order(this.direction, this.property, this.ignoreCase, nullHandling);
        }

        public MySort.Order nullsFirst() {
            return this.with(MySort.NullHandling.NULLS_FIRST);
        }

        public MySort.Order nullsLast() {
            return this.with(MySort.NullHandling.NULLS_LAST);
        }

        public MySort.Order nullsNative() {
            return this.with(MySort.NullHandling.NATIVE);
        }

        public MySort.NullHandling getNullHandling() {
            return this.nullHandling;
        }

        @Override
        public int hashCode() {
            int result = 17;
            result = 31 * result + this.direction.hashCode();
            result = 31 * result + this.property.hashCode();
            result = 31 * result + (this.ignoreCase ? 1 : 0);
            result = 31 * result + this.nullHandling.hashCode();
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            } else if (!(obj instanceof MySort.Order)) {
                return false;
            } else {
                MySort.Order that = (MySort.Order) obj;
                return this.direction.equals(that.direction) && this.property.equals(that.property) && this.ignoreCase == that.ignoreCase && this.nullHandling.equals(that.nullHandling);
            }
        }

        @Override
        public String toString() {
            String result = String.format("%s: %s", new Object[]{this.property, this.direction});
            if (!MySort.NullHandling.NATIVE.equals(this.nullHandling)) {
                result = result + ", " + this.nullHandling;
            }

            if (this.ignoreCase) {
                result = result + ", ignoring case";
            }

            return result;
        }
    }

    public static enum NullHandling {
        NATIVE,
        NULLS_FIRST,
        NULLS_LAST;

        private NullHandling() {
        }
    }

    public static enum Direction {
        ASC,
        DESC;

        private Direction() {
        }

        public boolean isAscending() {
            return this.equals(ASC);
        }

        public boolean isDescending() {
            return this.equals(DESC);
        }

        public static MySort.Direction fromString(String value) {
            try {
                return valueOf(value.toUpperCase(Locale.US));
            } catch (Exception var2) {
                throw new IllegalArgumentException(String.format("Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", new Object[]{value}), var2);
            }
        }

        public static MySort.Direction fromStringOrNull(String value) {
            try {
                return fromString(value);
            } catch (IllegalArgumentException var2) {
                return null;
            }
        }
    }


}
